package com.example.dao;

import com.example.entity.Admin;
import com.example.entity.PageInfo;
import com.example.entity.Student;
import com.example.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 管理员对象数据访问类
 * @author 李平
 * @date 2022-5-2
 */
@Component
public class AdminDao {

    @Autowired
    @Qualifier("apiJdbcTemplate")
    protected JdbcTemplate jdbcTemplate;

    private static final Logger log = LoggerFactory.getLogger(AdminDao.class.toGenericString());

    /**
     * 通过电话号码和密码查询管理员信息
     * @param name
     * @param password
     * @return Admin
     */
    public Admin getAdminByPAndP(String name, String password){

        String sql = String.format("SELECT * FROM admin where name='%s' and password='%s'", name, password);
        List<Admin> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Admin.class));
        log.info(sql);
        return query.size() == 0 ? null : query.get(0);
    }

    /**
     * 通过name查询管理员信息
     * @param name
     * @return Student
     */
    public Admin getAdminByPhone(String name){
        String sql = String.format("SELECT * FROM admin where name='%s'", name);
        List<Admin> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Admin.class));
        log.info(sql);
        return query.size() == 0 ? null : query.get(0);
    }

    public List<Student> getAllStudent(PageInfo info){
        String sql;
        int size = info.getPage_size();
        int index = info.getPage_index();
        String search = info.getSearch();
        int start = size*(index-1);
        log.info(search);

        if(StringUtil.isNotEmpty(info.getSearch())) {
            sql = String.format("SELECT * FROM student where phone like '%s' LIMIT %d,%d",StringUtil.formatLike(search),start,size);
        }
        else {
            sql = String.format("SELECT * FROM student LIMIT %d,%d",start,size);
        }
        List<Student> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class));
        return query;
    }

    public Integer getCount(){
        String sql = "SELECT count(*) FROM student";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        if (list.size() > 0){
            return Integer.parseInt(StringUtil.getNumber(list.get(0).toString()));
        }
        return 0;
    }

    public Integer getCount(String search){
        String sql = String.format("SELECT count(*) FROM student where phone like '%s'",StringUtil.formatLike(search));
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        if (list.size() > 0){
            return Integer.parseInt(StringUtil.getNumber(list.get(0).toString()));
        }
        return 0;
    }


    public Boolean deleteStudent(String id) {
        String SQL = String.format("delete from student where id = '%s'",id);
        jdbcTemplate.execute(SQL);
        return true;
    }
}
